﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using System.Text;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class BillAssign : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TaskType.Items.Add(new ListItem("--不限--", "0"));
                TaskType.Items.Add(new ListItem("保养", "3"));
                TaskType.Items.Add(new ListItem("校准", "6"));
                TaskType.Items.Add(new ListItem("-回访", "7"));
                TaskType.SelectedValue = "0";
                BindList(1);
            }
        }
        private void BindList(int pageindex)
        {
            //List<SqlDbParameter> parms = new List<SqlDbParameter>();
            StringBuilder sb = new StringBuilder();

            string Whereclause = " and TaskStatus = 0  ";
            int RecordCount = 0;
            RecordCount = new BudgetBll().getDataright(base.CurrentUserID.ToString());

            if (RecordCount == 0)
            { sb.Append(" and [PerformPerson] ='").Append(base.CurrentUserID).Append("'"); }
            else
            { sb.Append(" and [PerformPerson] in(").Append("select userid from vw_sysUser where DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))"); }

            Whereclause += sb.ToString();
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause = " and CustomerID in ( select CustomerID from vw_CustomerAllInfo where CustomerName like '%" + txtContractCodeBasic.Text.Trim() + "%' or  CustomerArea like '%" + txtContractCodeBasic.Text.Trim() + "%')";
            }
            if (this.txtInstrument.Text.Trim() != "")
            {
                Whereclause += " and InstrumentID in ( select InstrumentID from Cms_Wsm_InstrumentInfo where InstrumentName like '%" + txtInstrument.Text.Trim() + "%')";
            }

            Whereclause += " and IsSystemAssign = 1";
            if (TaskType.SelectedValue.ToString() != "0")
            {
                Whereclause += " and TaskType = " + TaskType.SelectedValue.ToString();
            }
            if (txtPerformPerson.Text != "")
            {
                Whereclause += " and [PerformPerson] in(select userid from cms_sys_User where UserName like '%" + txtPerformPerson.Text.Trim() + "%')";
            }
           
            
            RecordCount = 0;
            List<WsmTaskRegisterEntity> list = new SysUserBll().GetAll<WsmTaskRegisterEntity>(Whereclause, "RegisterDate desc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterUsers.DataSource = list;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindList(1);
            }
            if (e.CommandName == "complete")
            {
                Label RegisterID = RepeaterUsers.Items[e.Item.ItemIndex].FindControl("RegisterID") as Label;
                WsmTaskRegisterEntity entity = new SystemBll().Get<WsmTaskRegisterEntity>(new Guid(RegisterID.Text));
                entity.TaskStatus = 1;
                new SystemBll().Update(entity);
            }
            BindList(1);
        }
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(1);
        }
        protected void btnAssign_Click(object sender, EventArgs e)
        {
            for (int index = 0; index < RepeaterUsers.Items.Count; index++) {
                CheckBox isCheck = RepeaterUsers.Items[index].FindControl("isCheck") as CheckBox;
                Label RegisterID = RepeaterUsers.Items[index].FindControl("RegisterID") as Label;
                if (isCheck.Checked) {
                    WsmTaskRegisterEntity entiy = new SystemBll().Get<WsmTaskRegisterEntity>(new Guid(RegisterID.Text));
                    entiy.PerformPerson = new Guid(employeeGUID.Value);
                    new SystemBll().Update(entiy);
                }
            }
            BindList(1);
        }

        protected void RepeaterUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label CustomerID = e.Item.FindControl("CustomerID") as Label;
                Label CustomerName = e.Item.FindControl("CustomerName") as Label;
                Label PerformPerson = e.Item.FindControl("PerformPerson") as Label;
                Label PerformPersonName = e.Item.FindControl("PerformPersonName") as Label;
                Label Agent = e.Item.FindControl("Agent") as Label;
                Label TaskType = e.Item.FindControl("TaskType") as Label;
                Label TaskTypeName = e.Item.FindControl("TaskTypeName") as Label;
                Label CustomerArea = e.Item.FindControl("CustomerArea") as Label;
                Label InstrumentID = e.Item.FindControl("InstrumentID") as Label;
                Label Instrumentname = e.Item.FindControl("Instrumentname") as Label;

                if (CustomerID != null)
                {
                    CtrtCustomerEntity entity = new SystemBll().Get<CtrtCustomerEntity>(new Guid(CustomerID.Text));
                    CustomerName.Text = entity.CustomerName;
                
                    if (PerformPerson.Text.Trim() != "")
                    {
                        PerformPersonName.Text = new SystemBll().Get<SysUserEntity>(new Guid(PerformPerson.Text)).UserName;
                    }
                    TaskTypeName.Text = EnumeratorHelper.GetFieldText((Enumerator.SalesService)Convert.ToInt32(TaskType.Text));
                

                    WsmCustomerExtensionEntity extensionEntity = new SystemBll().Get<WsmCustomerExtensionEntity>(new Guid(CustomerID.Text));
                    if (extensionEntity != null)
                        CustomerArea.Text = extensionEntity.CustomerArea;

                    WsmInstrumentInfoEntity insurment = new SystemBll().Get<WsmInstrumentInfoEntity>(new Guid(InstrumentID.Text));
                    if (insurment != null)
                        Instrumentname.Text = insurment.instrumentName;
                }
            }
        }
    }
}
